package com.jiao.databinding5;

import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.jiao.databinding5.databinding.ActivityMainBinding;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        MyViewModel viewModel = new ViewModelProvider(this, new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(MyViewModel.class);
//        MyViewModel viewModel2 =  new ViewModelProvider(this).get(MyViewModel.class);

        activityMainBinding.setViewModel(viewModel);

        //DataBinding需调用setLifecycleOwner(LifecycleOwner lifecycleOwner)之后，
        // 绑定了LiveData数据源的xml控件才会随着数据变化而改变。
        activityMainBinding.setLifecycleOwner(this);

        viewModel.getaTeamScore().observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {

            }
        });
    }
}